﻿using HamcoWcs.Entitys.BaseConfigs;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HamcoWcs.Models.Monitors
{
    public class MonitorSrmData
    {
        public string Aisle { get; set; }
        public int DeviceMode { get; set; }
        public int ErrorCode { get; set; }
        public int IsTask { get; set; }
        public int IsGoods { get; set; }
        public string UnitId { get; set; } = "";
        public int CurrentBay { get; set; }
        public string Name { get; set; }
        public string Des { get; set; }
        public int BcrState { get; set; }
        public int ServoState { get; set; }
        public int Enabled { get; set; }
        public ushort DestinationBay { get; set; }
        public string Speed { get; set; }
        public ulong Input { get; set; }
        public uint Output { get; set; }
        public string Request { get; set; }
        public string GlobalPara { get; set; }
        public List<ulong> Inputs { get; set; } = new List<ulong>();
        public List<uint> Outputs { get; set; } = new List<uint>();

        public MonitorSrmData()
        {

        }
        public MonitorSrmData(SrmDto srm)
        {
            Name = $"{srm.MachineName}-{srm.PortName}";
            Aisle = srm.Aisle.NodeName;
            Update(srm);
        }
        public void Update(SrmDto srm)
        {
            DeviceMode = srm.DeviceMode;
            ErrorCode = srm.ErrorCode;
            IsTask = srm.IsTask;
            IsGoods = srm.IsGoods;
            UnitId = srm.UnitId;
            CurrentBay = srm.CurrentBay;
            BcrState = srm.BcrState;
            ServoState = srm.ServoState;
            Des = GetDes(srm);
            GlobalPara = srm.GlobalPara;
            Enabled = srm.Enabled;
            DestinationBay = srm.DestinationBay;

            Speed = $"{((double)srm.Speed / 1000)}m/min";

            Input = BitConverter.ToUInt64(srm.IOInfo.Take(8).ToArray());
            Output = BitConverter.ToUInt32(srm.IOInfo.Skip(8).Take(4).ToArray());
#if DEBUG
            Input = ((ulong)new Random().Next() << 32) + (ulong)new Random().Next();
            Output = (uint)new Random().Next();
#endif


            if (Inputs.Count >= 30) Inputs.RemoveAt(0);
            Inputs.Add(Input);
            if (Outputs.Count >= 30) Outputs.RemoveAt(0);
            Outputs.Add(Output);
            Request = ((EnumSrmRecvMsg)srm.Request).ToString();
        }
        private string GetDes(SrmDto srm)
        {
            switch (srm.DeviceMode)
            {
                case 0: return "设备离线";
                case 1: return "设备运行";
                case 2: return "设备手动";
                case 3: return "设备停止";
                case 4: return GetAlarm(srm.ErrorCode.ToString());
                case 5: return "设备维护";
                case 6: return "管制中";
                case 8: return "禁用";
                default: return "未知状态";
            }
        }


        private string GetAlarm(string code)
        {
            var a = Alarms.FirstOrDefault(x => x.Module == "srm" && x.Code == code);
            if (a is null) return "未知异常";
            return a.AlarmName;
        }

        public static List<AlarmExplanation> Alarms = new List<AlarmExplanation>();

        public static void RefreshAlarms(List<AlarmExplanation> alarms)
        {
            Alarms.Clear();
            Alarms.AddRange(alarms);
        }
    }
}
